import constant from "./constant";

// 存储变量名
let storageKey = "storage_data";

// 存储节点变量名
let storageNodeKeys = [
  constant.tagList,
  constant.gameContral,
  constant.general,
  constant.tactic,
  constant.stratagem,
  constant.politics,
  constant.land,
  constant.mainControl,
  constant.map,
  constant.playerContral,
  constant.command,
  constant.historyInfo,
];

const storage = {
  set: function (key, value) {
    if (storageNodeKeys.indexOf(key) != -1) {
      let tmp = localStorage.getItem(storageKey);
      tmp = tmp ? tmp : JSON.stringify({});
      tmp = JSON.parse(tmp);
      tmp[key] = value;
      localStorage.setItem(storageKey, JSON.stringify(tmp));
    }
  },
  get: function (key) {
    let storageData = localStorage.getItem(storageKey) || JSON.stringify({});
    let res = JSON.parse(storageData);
    return res[key] || "";
  },
  remove: function (key) {
    let storageData = localStorage.getItem(storageKey) || JSON.stringify({});
    let res = JSON.parse(storageData);
    delete res[key];

    localStorage.setItem(storageKey, JSON.stringify(res));
  },
  clean: function () {
    localStorage.removeItem(storageKey);
  },
};

export default storage;
